package co.fitstart.admin.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.persistence.annotation.Dynamic;
import com.demo2do.core.support.Page;
import com.demo2do.core.utils.DateFormatUtils;
import com.demo2do.core.utils.DateUtils;
import co.fitstart.admin.dao.SatisfactionSurveyDao;
import co.fitstart.admin.entity.report.CourseScheduleCount;
import co.fitstart.admin.entity.search.SatisfactionSurveySearch;
import co.fitstart.admin.service.SatisfactionSurveyService;
import co.fitstart.entity.coupon.Coupon;
import co.fitstart.entity.coupon.CouponStatus;
import co.fitstart.entity.coupon.DiscountType;
import co.fitstart.entity.course.round.CourseRound;
import co.fitstart.entity.market.satisfaction.SatisfactionSurvey;
import co.fitstart.entity.site.Site;
import co.fitstart.entity.user.Student;

/**
 * 
 * @author haiyan_xiao
 *
 */
@Service("satisfactionSurveyService")
public class SatisfactionSurveyServiceImpl implements SatisfactionSurveyService {

    @Dynamic
    private SatisfactionSurveyDao satisfactionSurveyDao;

    @Autowired
    private GenericDaoSupport genericDaoSupport;

    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.SatisfactionSurveyService#listSatisfactionSurveySearch(co.fitstart.admin.entity.search.SatisfactionSurveySearch)
     */
    @SuppressWarnings("unchecked")
    public List<CourseScheduleCount> listSatisfactionSurveySearch(SatisfactionSurveySearch search) {
        List<Student> students = satisfactionSurveyDao.listStudent(DateUtils.addMonths(new Date(), -2));
        if(!students.isEmpty()) {
            search.setExcludedStudents(students);
        }
        
        String s = search.buildSentenceTemplate();
        Map<String, Object> parameters = search.buildParameters();
        return genericDaoSupport.searchForList(s, parameters);
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.SatisfactionSurveyService#send(java.lang.Long[])
     */
    public void send(Long[] courseRoundIds) {
        if(courseRoundIds != null && courseRoundIds.length > 0) {
            for(Long courseRoundId : courseRoundIds) {
                CourseRound courseRound = genericDaoSupport.load(CourseRound.class, courseRoundId);
                
                if(courseRound != null) {
                    SatisfactionSurvey survey = new SatisfactionSurvey(courseRound);
                    genericDaoSupport.save(survey);
                }
            }
        }
        
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.SatisfactionSurveyService#list(co.fitstart.entity.site.Site, java.lang.Boolean, com.demo2do.core.support.Page)
     */
    public List<SatisfactionSurvey> list(Site site, Boolean complete, Page page) {
        List<SatisfactionSurvey> result = new ArrayList<>();
        
        if(site == null) {
            if(complete == null) {
                result = satisfactionSurveyDao.list(page.getBeginIndex(), page.getMaxResultRecords());
            } else {
                if(complete) {
                    result = satisfactionSurveyDao.listComplete(page.getBeginIndex(), page.getMaxResultRecords());
                } else {
                    result = satisfactionSurveyDao.listUncomplete(page.getBeginIndex(), page.getMaxResultRecords());
                }
            }
        } else {
            if(complete == null) {
                result = satisfactionSurveyDao.list(site, page.getBeginIndex(), page.getMaxResultRecords());
            } else {
                if(complete) {
                    result = satisfactionSurveyDao.listComplete(site, page.getBeginIndex(), page.getMaxResultRecords());
                } else {
                    result = satisfactionSurveyDao.listUncomplete(site, page.getBeginIndex(), page.getMaxResultRecords());
                }
            }
        }
        
        page.calculate(result.size());
        return result.size() > page.getEveryPage() ? result.subList(0, page.getEveryPage()) : result;
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.SatisfactionSurveyService#list(java.util.List, java.lang.Boolean, com.demo2do.core.support.Page)
     */
    public List<SatisfactionSurvey> list(List<Site> sites, Boolean complete, Page page) {
        List<SatisfactionSurvey> result = new ArrayList<>();
        
        if(complete == null) {
            result = satisfactionSurveyDao.list(sites, page.getBeginIndex(), page.getMaxResultRecords());
        } else {
            if(complete) {
                result = satisfactionSurveyDao.listComplete(sites, page.getBeginIndex(), page.getMaxResultRecords());
            } else {
                result = satisfactionSurveyDao.listUncomplete(sites, page.getBeginIndex(), page.getMaxResultRecords());
            }
        }
        
        page.calculate(result.size());
        return result.size() > page.getEveryPage() ? result.subList(0, page.getEveryPage()) : result;
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.SatisfactionSurveyService#load(java.lang.Long)
     */
    public SatisfactionSurvey load(Long surveyId) {
        return genericDaoSupport.load(SatisfactionSurvey.class, surveyId);
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.SatisfactionSurveyService#listComplete(co.fitstart.entity.site.Site, java.util.Date, java.util.Date)
     */
    public List<SatisfactionSurvey> listComplete(Site site, Date begin, Date end) {
        Date beginDate = DateUtils.parseDate(DateFormatUtils.format(begin, "yyyy-MM-dd 00:00:00"), "yyyy-MM-dd HH:mm:ss");
        Date endDate = DateUtils.parseDate(DateFormatUtils.format(end, "yyyy-MM-dd 23:59:59"), "yyyy-MM-dd HH:mm:ss");
        
        if(site == null) {
            return satisfactionSurveyDao.listComplete(beginDate, endDate);
        } else {
            return satisfactionSurveyDao.listComplete(site, beginDate, endDate);
        }
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.SatisfactionSurveyService#listUsedGiftCoupon(co.fitstart.entity.site.Site, java.util.Date, java.util.Date)
     */
    public List<SatisfactionSurvey> listUsedGiftCoupon(Site site, Date begin, Date end) {
        Date beginDate = DateUtils.parseDate(DateFormatUtils.format(begin, "yyyy-MM-dd 00:00:00"), "yyyy-MM-dd HH:mm:ss");
        Date endDate = DateUtils.parseDate(DateFormatUtils.format(end, "yyyy-MM-dd 23:59:59"), "yyyy-MM-dd HH:mm:ss");
        
        if(site == null) {
            return satisfactionSurveyDao.listUsedGiftCoupon(DiscountType.GIFT, CouponStatus.USED, beginDate, endDate);
        } else {
            return satisfactionSurveyDao.listUsedGiftCoupon(DiscountType.GIFT, CouponStatus.USED, site, beginDate, endDate);
        }
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.SatisfactionSurveyService#listUsedGiftCoupon(java.util.List, java.util.Date, java.util.Date)
     */
    public List<SatisfactionSurvey> listUsedGiftCoupon(List<Site> sites, Date begin, Date end) {
        Date beginDate = DateUtils.parseDate(DateFormatUtils.format(begin, "yyyy-MM-dd 00:00:00"), "yyyy-MM-dd HH:mm:ss");
        Date endDate = DateUtils.parseDate(DateFormatUtils.format(end, "yyyy-MM-dd 23:59:59"), "yyyy-MM-dd HH:mm:ss");
        
        return satisfactionSurveyDao.listUsedGiftCoupon(DiscountType.GIFT, CouponStatus.USED, sites, beginDate, endDate);
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.SatisfactionSurveyService#list(java.lang.String)
     */
    public List<SatisfactionSurvey> list(String couponCode) {
        // return satisfactionSurveyDao.list(couponCode, DiscountType.GIFT);
        List<Coupon> coupons = satisfactionSurveyDao.list(couponCode);
        
        if(CollectionUtils.isNotEmpty(coupons)) {
            return satisfactionSurveyDao.list(coupons);
        } else {
            return null;
        }
    }
    
    public List<SatisfactionSurvey> list() {
        return satisfactionSurveyDao.list(DiscountType.GIFT);
    }
}
